package com.wyx.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.exceptions.ClientException;

import java.io.FileInputStream;
import java.io.InputStream;

public class AliOssUtil {
    private static final String ENDPOINT = "https://oss-cn-beijing.aliyuncs.com";
    private static final String ACCESS_KEY_ID = "LTAI5tDGZrCwFbb8Sm2rEthS";
    private static final String ACCESS_KEY_SECRET = "tqpQmLNHmfuOscDIPmUMYobSoCBqtO";
    private static final String BUCKET = "202502271607";
    public static String upload(String objectName, InputStream inputStream) throws ClientException {
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        try{
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, objectName, inputStream);
            PutObjectResult result=ossClient.putObject(putObjectRequest);
            return "https://"+BUCKET+"."+ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+objectName;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return null;
    }
}
